﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Service;
using QXBook.Model;
using System.Web.UI.HtmlControls;
using QXBook.Utils;
using QXBook.Controls.HtmlControls;

namespace Web.SysManage.Video
{
    public partial class VideoList : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //检测管理员登录
            ServiceFactory.CreateInstance<AdminService>().CheckAdminLogin();
            ServiceFactory.CreateInstance<AdminService>().CheckAdminPop(32);

            lmc.AddEditLinkUrl = "VideoEdit.aspx";
            lmc.EnableLinkOn = false;
            lmc.EnableLinkOff = false;
            lmc.EnableLinkShipments = false;
            lmc.EnableLinkResave = false;
            if (!Page.IsPostBack)
            {
                VideoCondition condition = GetPageCondition();
                this.TextBind(condition);
            }

            lmc.EditHandler += new EventHandler(lmc_EditHandler);
            lmc.DeleteHandler += new EventHandler(DeleteMethod);
            lbtnAll.Click += new EventHandler(lbtnAll_Click);
            lbtnIsPublish.Click += new EventHandler(lbtnIsPublish_Click);
            lbtnNoPublish.Click += new EventHandler(lbtnNoPublish_Click);
        }

        void lmc_EditHandler(object sender, EventArgs e)
        {
            int flags = 0;
            int memberID = 0;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    memberID = CommonUtils.ToInt(hidID.Value);
                    flags++;
                }
            }

            if (flags == 1)
            {
                Response.Redirect("VideoEdit.aspx?VideoID=" + memberID);
            }
            else if (flags > 1)
            {
                base.ResponseErch("只能选择一条");
            }
            else
            {
                base.ResponseErch("请选择一条视频");
            }
        }

        void DeleteMethod(object sender, EventArgs e)
        {
            int flags = 0;
            string videos = string.Empty;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox CheckID = (HtmlInputCheckBox)item.FindControl("CheckID");
                if (CheckID.Checked)
                {
                    //ServiceFactory.CreateInstance<AdminRuleService>().r
                    HtmlInputHidden hidID = (HtmlInputHidden)item.FindControl("hidID");
                    videos += hidID.Value + ",";
                    flags++;
                }
            }

            if (flags > 0)
            {
                ServiceFactory.CreateInstance<VideoService>().DeleteList(videos);
                VideoCondition condition = GetPageCondition();
                this.TextBind(condition);

                base.ResponseErch("删除成功");
            }
            else
            {
                base.ResponseErch("请选择一个或者多个");
            }
        }

        void lbtnAll_Click(object sender, EventArgs e)
        {
            hidIsPublish.Value = string.Empty;
            VideoCondition condition = GetPageCondition();
            this.TextBind(condition);
        }

        void lbtnIsPublish_Click(object sender, EventArgs e)
        {
            hidIsPublish.Value = CommonUtils.ToInt(YesNo.Yes).ToString();
            VideoCondition condition = GetPageCondition();
            this.TextBind(condition);
        }

        void lbtnNoPublish_Click(object sender, EventArgs e)
        {
            hidIsPublish.Value = CommonUtils.ToInt(YesNo.No).ToString();
            VideoCondition condition = GetPageCondition();
            this.TextBind(condition);
        }

        private VideoCondition GetPageCondition()
        {
            VideoCondition condition = new VideoCondition();
            if (!string.IsNullOrEmpty(hidIsPublish.Value))
            {
                condition.IsPublish = (YesNo)CommonUtils.ToInt(hidIsPublish.Value);
            }

            return condition;
        }

        private void TextBind(VideoCondition condition)
        {
            PagerState ps = new PagerState();
            if (string.IsNullOrEmpty(CommonUtils.Get("page")))
            {
                ps.CurrentPage = 1;
            }
            else
            {
                ps.CurrentPage = CommonUtils.ToInt(CommonUtils.Get("page"));
            }
            ps.PageSize = 20;

            List<VideoInfo> list = ServiceFactory.CreateInstance<VideoService>().GetModelList(ref ps, condition);

            this.rptList.DataSource = list;
            this.rptList.DataBind();

            pager.CurrentPage = ps.CurrentPage;
            pager.Per = ps.PageSize;
            pager.MaxPage = ps.PageCount;
            pager.Total = ps.RecordCount;
        }

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            VideoInfo article = (VideoInfo)e.Item.DataItem;
            Span sId = (Span)e.Item.FindControl("sId");
            HtmlInputHidden hidID = (HtmlInputHidden)e.Item.FindControl("hidID");
            Span sTitle = (Span)e.Item.FindControl("sTitle");
            Span sAddDate = (Span)e.Item.FindControl("sAddDate");

            sId.Text = article.VideoID.ToString();
            hidID.Value = article.VideoID.ToString();
            sTitle.Text = article.Title;
            sAddDate.Text = article.AddDate.ToString("yyyy-MM-dd");
        }
    }
}